# -*- coding: utf-8 -*-
# created on 2016/7/22
from mathsolver.functions.jihe.jihe_operate import JiHeOperate
from mathsolver.functions.base import *
from mathsolver.functions.hanshu.lingdian_geshu import LingDianGeShuQiuCan


class JiHeTof(BaseFunction):
    @staticmethod
    def check_empty(res_v):
        if (isinstance(res_v, list) and len(res_v) == 0) or (isinstance(res_v, Set) and res_v.is_EmptySet is True):
            return True
        else:
            return False

    @staticmethod
    def check_unival(res_v):
        if res_v == S.Reals:
            return True
        else:
            return False

    def check_equal(self, left_v, right):
        if right in ["ph", "∅"]:
            answer = self.check_empty(left_v)
        elif right == "R":
            answer = self.check_unival(left_v)
        else:
            if isinstance(right, dict):
                right_v = right["conditions"]
                if 'set' in right_v[0]:
                    right_v = right_v[0]['set']
            else:
                right_set = self.search(right)
                right_v = right_set.value["value"]

            if isinstance(right_v, list):
                right_v = FiniteSet(*right_v)

            if right_v == left_v:
                answer = True
            else:
                answer = False
        return answer

    def solver(self, *args):
        left, op, right = args[0].value
        answer = None
        if isinstance(left, list):
            self.label.add("已知集合判断集合运算关系")
            try:
                stepsolver = JiHeOperate(known=self.known).solver(BaseSetOp(left))
                res = stepsolver.output[0]
                res_v = res.value["value"]
                answer = self.check_equal(res_v, right)
            except Exception:
                pass

        elif isinstance(left, str) and op == "=" and isinstance(right, str):
            left_set = self.search(left)
            left_v = left_set.value["value"]

            answer = self.check_equal(left_v, right)
            self.label.add("判断集合相等")

        elif isinstance(left, str) and op == "⊆" and isinstance(right, str):
            self.label.add("判断集合间的关系")
            left_set = self.search(left)
            left_v = left_set.value["value"]

            if right in ["ph", "∅"]:
                self.label.add("判断集合为空集")
                answer = self.check_empty(left_v)
            elif right == "R":
                answer = True
            else:
                right_set = self.search(right)
                right_v = right_set.value["value"]
                if isinstance(right_v, list):
                    right_v = FiniteSet(*right_v)

                if isinstance(left_v, list):
                    left_v = FiniteSet(*left_v)

                if isinstance(left_v, Set) and isinstance(right_v, Set):
                    if left_v.intersection(right_v) == left_v:
                        answer = True
                    else:
                        answer = False
        elif isinstance(left, str) and op in ["⊂", "⊊"] and isinstance(right, str):
            self.label.add("判断集合间的关系")
            left_set = self.search(left)
            left_v = left_set.value["value"]

            if right in ["ph", "∅"]:
                answer = False
            elif right == "R":
                answer = not self.check_unival(left_v)
            else:
                right_set = self.search(right)
                right_v = right_set.value["value"]
                if isinstance(left_v, Set) and isinstance(right_v, Set):
                    if left_v.intersection(right_v) == left_v and left_v != right_v:
                        answer = True
                    else:
                        answer = False

        # if answer is None:
        #     raise ValueError("unexpected left %s" % left)

        self.output.append(BaseVariable(answer))

        return self


class JiHeTofBelong(BaseFunction):
    def solver(self, *args):
        belong = args[0]
        k = belong.value[0]
        v = belong.value[2]
        innerset = self.search(v)
        if sympify(k) in innerset.value["value"]:
            self.output.append(BaseVariable(True))
        else:
            self.output.append(BaseVariable(False))
        self.label.add("判断集合元素和集合的关系")
        return self


class YuanSuGeShuQiuCan(BaseFunction):
    def solver(self, *args):
        tt, number = args[:2]
        eq = [item['eq'] for item in tt.value["conditions"] if 'eq' in item]
        assert len(eq) == 1
        if number.find("一个") >= 0:
            number = BaseNumber("1")
        else:
            raise Exception("to do")
        return LingDianGeShuQiuCan().solver(BaseEq(["y", eq[0][0]]).eq2func(), number)


if __name__ == '__main__':
    pass
